package com.huatek.framework.action;

import com.huatek.framework.entity.FwSrcAction;
import com.huatek.framework.security.ThreadLocalClient;
import com.huatek.framework.util.Parameter;

public class OperationButton {
	/*private static String getExportButton(String extName, String typeName,
			String fileName, String imagePath) {
		return "<a href=\"javascript:export" + typeName + "('" + fileName
				+ "')\"><img src=\"" + imagePath + "table/compact/" + extName
				+ ".gif\"  style=\"border:0\"  title=\"Export " + typeName
				+ "\"  alt=\"Export " + typeName + "\" /></a>";
	}*/
	public static String general(String contextPath){
		if(ThreadLocalClient.get().getSrcAction()==null){
			return "";
		}
		String imagePath = contextPath+ Parameter.getInstance().getProp().getProperty("buttonToolImagePath");
		String buttonSpace = Parameter.getInstance().getProp().getProperty("buttonSpace","&nbsp;&nbsp;&nbsp;&nbsp;");
		String histroyBack = "<a href=\"javascript:backMainPage('"+ThreadLocalClient.get().getSrcAction().getFwSource().getSourceCode()+"')\"><img src=\""
		+ imagePath
		+ Parameter.getInstance().getProp().getProperty("buttonBackImage", "an-12.gif")
		+ "\" style=\"border:0\" title='Back' alt='Back'></a>";
		StringBuffer buttongString = new StringBuffer("");
		String method = ThreadLocalClient.get().getActionMethod();
		if (!method.startsWith("query")) {
			buttongString.append("<a href=\"javascript:"
			+ Parameter.getInstance().getProp().getProperty("buttonSaveFunction", "saveData")
			+ "()\"><img src=\"" + imagePath
			+ Parameter.getInstance().getProp().getProperty("buttonSaveImage", "an-01.gif")
			+ "\" style=\"border:0\"  title='Save' alt='Save'></a>");
			 return buttonSpace + buttongString + buttonSpace + histroyBack;

		}
		String[] operationName = Parameter.getInstance().getProp().getProperty("buttonToolName").split(",");
		String[] imageOrder = Parameter.getInstance().getProp().getProperty("buttonToolImage").split(",");
		String[] functionName = Parameter.getInstance().getProp().getProperty("buttonToolFunction").split(",");
		String export = "";
		for (int i = 0; i < operationName.length; i++) {
			for (int k = 0; k < ThreadLocalClient.get().getPermitAction().size(); k++) {
				FwSrcAction srcAction = ThreadLocalClient.get().getPermitAction().get(k);
				if (operationName[i].equalsIgnoreCase(srcAction.getMethodName())) {
					String fileName = srcAction.getFwSource().getSourceCode();
					fileName = fileName.substring(0,fileName.lastIndexOf("."));
					/*if (operationName[i].endsWith(Parameter.getInstance().getProp().getProperty("buttonQueryName", "Query"))&& ThreadLocalClient.get().getSrcAction().equals(srcAction)) {
						export = getExportButton("xls", "Excel",fileName, imagePath);
						export += buttonSpace+ getExportButton("csv", "CSV",fileName, imagePath);
					}*/
					buttongString.append(buttonSpace);
					buttongString.append("<a href=\"javascript:"
					+ functionName[i] + "('" +fileName+".do"+ "')\"><img src=\""
					+ imagePath + imageOrder[i]
					+ "\" style=\"border:0\" title='"+operationName[i]+"' alt='"+operationName[i]+"'></a>");

				}
			}
		}
		return buttongString+ buttonSpace + export;
	}
}
